{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datasketch import MinHash, MinHashLSHEnsemble\n",
    "data1 = ['这个', '程序', '代码', '太乱', '那个', '代码', '规范']\n",
    "data2 = ['这个', '程序', '代码', '不', '规范', '那个', '更', '规范']\n",
    "data3 = ['这个', '程序', '代码', '不', '规范', '那个', '规范', '些']\n",
    "##造了三个已经分好词了句子，并用list存储"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<datasketch.minhash.MinHash object at 0x7f9d3cd88d50> <class 'datasketch.minhash.MinHash'>\n",
      "<datasketch.minhash.MinHash object at 0x7f9d3cc64810> <class 'datasketch.minhash.MinHash'>\n",
      "<datasketch.minhash.MinHash object at 0x7f9d3cc64890> <class 'datasketch.minhash.MinHash'>\n"
     ]
    }
   ],
   "source": [
    "# 创建MinHash对象\n",
    "m1 = MinHash()\n",
    "m2 = MinHash()\n",
    "m3 = MinHash()\n",
    "for d in data1:\n",
    "    m1.update(d.encode('utf8'))\n",
    "for d in data2:\n",
    "    m2.update(d.encode('utf8'))\n",
    "for d in data3:\n",
    "    m3.update(d.encode('utf8'))\n",
    "print(m1,type(m1))    \n",
    "print(m2,type(m2))\n",
    "print(m3,type(m3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<datasketch.lshensemble.MinHashLSHEnsemble object at 0x7f9d3ca1e1d0> <class 'datasketch.lshensemble.MinHashLSHEnsemble'>\n"
     ]
    }
   ],
   "source": [
    "# 创建LSH Ensemble\n",
    "lshensemble = MinHashLSHEnsemble(threshold=0.8, num_perm=128)  #并且设定好了阈值\n",
    "print(lshensemble,type(lshensemble))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Index takes an iterable of (key, minhash, size)  同样先运行一下 index\n",
    "lshensemble.index([(\"m2\", m2, len(data2)), (\"m3\", m3, len(data3))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 判断lshensemble是否存在m2, m3\n",
    "print(\"m2\" in lshensemble)\n",
    "print(\"m3\" in lshensemble)\n",
    "print(\"m1\" in lshensemble)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "与m1相似度大于0.8的集合：\n",
      "m2\n",
      "m3\n"
     ]
    }
   ],
   "source": [
    "# 查询与m1相似度大于0.8的集合\n",
    "print(\"与m1相似度大于0.8的集合：\")\n",
    "for key in lshensemble.query(m1, len(data1)):\n",
    "    print(key)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
